import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class zuoye {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        job.setJobName("MR01");
        job.setJarByClass(zuoye.class);

        job.setMapperClass(MyMapper.class);
        job.setReducerClass(MyReducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        job.waitForCompletion(true);

        Job job2 = Job.getInstance(conf);
        job2.setJobName("MR02");
        job2.setJarByClass(zuoye.class);

        job2.setMapperClass(MyMapper2.class);
        job2.setReducerClass(MyReducer2.class);

        job2.setMapOutputKeyClass(Text.class);
        job2.setMapOutputValueClass(NullWritable.class);

        job2.setOutputKeyClass(NullWritable.class);
        job2.setOutputValueClass(Text.class);

        FileInputFormat.setInputPaths(job2, new Path(args[1]));
        FileOutputFormat.setOutputPath(job2, new Path(args[2]));

        job2.waitForCompletion(true);
    }
}

class MyMapper extends Mapper<LongWritable, Text, Text, Text> {
    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context) throws IOException, InterruptedException {
        String fileName = ((FileSplit)context.getInputSplit()).getPath().getName();
        String line = new String(value.getBytes(),0,value.getLength(),"GBK");
        String[] fields = line.split(",");
        if (fileName.contains("china_sites")) {
            int id;
            if ("PM2.5".equals(fields[2])) {
                for (int i = 3; i < fields.length; i++) {
                    if ("".equals(fields[i]) || fields[i] == null || " ".equals(fields[i])) {
                        fields[i] = "0";
                    }
                    id = 998 + i;
                    context.write(new Text(String.valueOf(id)), new Text("A"+" "+fields[0]+" "+fields[i]));
                }
            }
        }else{
            int id = Integer.parseInt(fields[0]) + 1000;
            context.write(new Text(String.valueOf(id)),new Text(fields[1]+" "+fields[2]+" "+fields[3]+" "+fields[4]));
        }

    }
}

class MyReducer extends Reducer<Text, Text, Text, Text> {
    @Override
    protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {
        String str = new String("");
        StringBuffer buffer = new StringBuffer("");
        for (Text value : values) {
            if(value.toString().contains("A")){
                String[] fields = value.toString().split(" ");
                buffer.append(fields[1]+"-"+fields[2]+" ");
            }else{
                str = value.toString();
            }
        }
        buffer.deleteCharAt(buffer.length()-1);
        context.write(new Text(key.toString()+" "+str),new Text(buffer.toString()));
    }
}

class MyMapper2 extends Mapper<LongWritable, Text, Text, NullWritable> {
    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, NullWritable>.Context context) throws IOException, InterruptedException {
        String[] fields = value.toString().split("\t");
        String[] s1 = fields[0].split(" ");
        if(s1.length==5){
            String[] s2 = fields[1].split(" ");
            String[] split1 = s2[0].split("-");
            String str = split1[0];
            int sum=0;
            for (String s : s2) {
                String[] splits = s.split("-");
                if(str.equals(splits[0])){
                    sum+=Integer.parseInt(splits[1]);
                }else{
                    context.write(new Text(s1[0]+" "+s1[1]+" "+s1[2]+" "+s1[3]+" "+s1[4]+" "+str+" "+(sum/24)),NullWritable.get());
                    str = splits[0];
                    sum = Integer.parseInt(splits[1]);
                }
            }
        }
    }
}

class MyReducer2 extends Reducer<Text,NullWritable, NullWritable, Text> {
    @Override
    protected void reduce(Text key, Iterable<NullWritable> values, Reducer<Text, NullWritable, NullWritable, Text>.Context context) throws IOException, InterruptedException {
        context.write(NullWritable.get(),key);
    }
}
